#include "stdio.h"
#include "frozen.h"
#include "parameter.h"
/*
 * Structure containing token type and value. Used in `json_walk()` and
 * `json_scanf()` with the format specifier `%T`.
 */
//struct json_token {
//    const char* ptr;           /* Points to the beginning of the value */
//    int len;                   /* Value length */
//    enum json_token_type type; /* Type of the token, possible values are above */
//};
static void scan_array(const char* str, int len, void* user_data) {
    struct json_token t;
    int i;
    char* buf = (char*)user_data;
    for (i = 0; json_scanf_array_elem(str, len, ".x", i, &t) > 0; i++) {
        sprintf(buf + strlen(buf), "%d[%.*s] ", i, t.len, t.ptr);
    }
}

static const char* test_scanf(void) {
    char buf[100] = "";
    int a = 0, b = 0;
    char* d = NULL;
    const char* str =
        "{ a: 1234, b : true, \"c\": {x: [17, 78, -20]}, d: \"hi%20there\" }";

    json_scanf(str, strlen(str), "{a: %d, b: %B, c: [%M], d: %Q}", &a, &b,
        &scan_array, buf, &d) ;
   
    return NULL;
}
void test_read_from_tcp()
{
    char msg[1024] = { 0 };
    int payload_len = 0;
    char* mid = "1";
    int timestamp = get_timestamp();
    char* type = "\"CMD_SYS_GETUPGRADE\"";
    unsigned char param[128] = { 0 };
    snprintf(param, 128, DEV_REQ_GET_PIECES, "\"xxx\"", 1);
    payload_len = snprintf(msg, 1024, DEV_REQUEST_BASE_DATA, mid,timestamp, type, param);
    char out[1024] = { 0 };
  //  json_setf(msg, 300, out, ".code", "%d",200);
    read_from_tcp(msg,1024);
}

int main()
{
   // test_scanf();
    char msg[1024];
    int ret = gen_dev_get_edgeid(msg, 1024);
    printf("%s\n",msg);
    memset(msg, 0, 1024);
    gen_dev_get_upgrade_file(msg, 1024,"xxx",1);
    printf("%s\n",msg);
    gen_dev_heart_beat(msg, 1024);
    printf("%s\n", msg);
    gen_dev_control(msg, 1024,"CMD_SYS_RESTART");
    printf("%s\n", msg);

    //getTimeStamp_yyyy();
    //test_read_from_tcp();
    Sleep(100000);
	return 0;
}